package com.xuxin.baby.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.XPopup;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.HistoryAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.contract.UserHistoryContract;
import com.xuxin.baby.database.DBControl;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.UserHistoryPresenter;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 历史足迹
 */
public class UserHistoryActivity extends BaseActivity implements UserHistoryContract.View {
    private final UserHistoryContract.Presenter presenter = new UserHistoryPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;
    private HistoryAdapter myAdapter;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("历史足迹");
        title_right.setImageResource(R.mipmap.img_delete);
        title_right.setVisibility(View.VISIBLE);
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                myAdapter.setList(DBControl.queryHistory());
                onComplete();
            }
        });
        RecycleUtils.init(smart_recycle);
        myAdapter = new HistoryAdapter();
        smart_recycle.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (0 == myAdapter.getData().get(position).getItemType()) {
                    Bundle bundle = new Bundle();
                    bundle.putString(BaseConstant.videoId, myAdapter.getData().get(position).getUid());
                    ActivityUtils.startActivity(bundle, VideoActivity.class);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString(BaseConstant.readerId, myAdapter.getData().get(position).getUid());
                    ActivityUtils.startActivity(bundle, ReaderActivity.class);
                }
            }
        });
    }

    @Override
    public void initData() {
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        dismiss();
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.title_right) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要清空全部浏览记录？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            try {
                                DBControl.deleteAllHistory();
                                popup.setTitle("正在删除中...").show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                myAdapter.setList(null);
                                onComplete();
                            }
                        }
                    })).show();
        }
    }
}